// 配置分页插件的目录
layui.extend({
    customPage: '../lib/customPage/custom-page',
    apiconfig: '../public/apiconfig'
});
layui.use(['form',"table","laypage","customPage","laydate"], function () {
    var form = layui.form,
        table = layui.table,
        laypage = layui.laypage,
        laydate = layui.laydate,
        customPage = layui.customPage //自定义分页插件
        element = layui.element;
        var mainTop=$(".main_top").outerHeight()
        $(".tableMb").height(window.screen.height-(230+mainTop+10));
        var loading;
         // 数据字典获取值--------------
        workSheet(form,'questionType',"work_sheet_type");
        form.on('select(questionType)', function(data){
            workSheetOnSelect(form,"work_sheet_type",data.value);
        })
        createDept(form,'createDept');
         // 数据字典获取值--------------
        // $.ajaxSetup({ cache: false })
        var searchstr={}
        customPageFun(searchstr);
        var tablereload= table.render({
            elem: '#tabList'
            ,title: '用户表'
            ,toolbar: '<div><a class="layui-btn layui-btn-radius " lay-event="accept" id="accept">接受</a><button type="button" class="layui-btn layui-btn-radius" lay-event="finish" id="finish">完成</button></div>'
            ,defaultToolbar: []
            // ,page: true //开启分页
            ,cols: [
                [{ type: 'checkbox', title: '选择',  width: 30},
                { type: 'numbers', title: '序号', width: 40},
                {field: 'workSheetCode',title: '工单编号',width:150,align:"center"}, 
                {field: 'questionType', title: '问题类型' ,align:"center"}, 
                {field: 'questionDesc', title: '问题描述',align:"center" }, 
                {field: 'createDate', title: '提交时间',width:150,align:"center"}, 
                {field: 'workSheetContacts', title: '联系人' ,align:"center"}, 
                {field: 'createDept', title: '处室',align:"center" }, 
                {field: 'workSheetPhone', title: '电话',width:150,align:"center"}, 
                {field: 'priority', title: '优先级',align:"center" , templet:function(d){
                    if(d.priority=='低'){
                        return "低"
                    }else if(d.priority=='高'){
                        return '<span class="priorityColor">'+d.priority+'</span>'
                    }else if(d.priority=='紧急'){
                        return '<span class="priorityColor">'+d.priority+'</span>'
                    }else{
                        return ''
                    }
                }}, 
                {field: 'statu', title: '状态',align:"center", templet:function(d){
                     if(d.statu=='已接收'){
                        return '<span class="statuColor">'+d.statu+'</span>'
                     }else{
                        return d.statu;
                     }
                } }, 
                {field: 'detailds', title: '详情',align:"center", width: 80,templet:function(){
                   return '<a class="layui-btn layui-btn-xs" data-title="查看" lay-event="LisyJobsLook">查看</a>'
                }}
                ]
            ],
            done: function(data) {
            }
        }); 
     //执行示例 如何采用 table.reload 方法分页的渲染放到 table之后
     function customPageFun(searchstr){
        customPage.render({
            elem: '#page',
            layout: ['skipFirst', 'prev', 'skip', 'next', 'skipLast', 'count'],
            url: 'gt/opm/workSheet/admin/wait/page',
            method: 'GET',
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            where: {
                workSheetCode:searchstr.workSheetCode,
                questionContent:searchstr.questionContent,
                questionType:searchstr.questionType,
                statu:searchstr.statu,
                priority:searchstr.priority,
                endDate:searchstr.endDate,
                startDate:searchstr.startDate,
                workSheetContacts:searchstr.workSheetContacts,
                createDept:searchstr.createDept
            },
            parseData:function(res){
                return {
                    "code":res.code, //解析接口状态
                    "flag":res.flag, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "data": res.data.rows, //解析数据列表
                    "total":res.data.total
                };
            },
            done: function(res, curr, data) {
                if(res.flag==true){
                    table.reload('tabList', {
                        data: res.data,
                        limit: res.size
                    });
                }else{
                    layer.alert(res.msg)
                }
                
            }
        });
    }
    // 监听列表点击事件
    table.on('tool(tabList)', function(obj){
       if(obj.event=="LisyJobsLook"){
          window.parent.xadmin.add_tab('工单待办详情','views/OrderDetail.html','',obj.data.id)
       }
    })
    // 监听toolbar点击事件
   table.on('toolbar(tabList)', function(obj){
        var checkStatus = table.checkStatus('tabList');
        var ids='',statu=''
        $.each(checkStatus.data,function(i,v){
            ids+=v.id+','
            statu+=v.statu
        })
        ids=ids.substr(0,ids.length-1);
        var str={"ids":ids};
        if(checkStatus.data!=''){
            if(obj.event=="accept"){
                if(statu.indexOf("1")=='-1'&&statu.indexOf("2")=='-1'&&statu.indexOf("3")=='-1'){
                    $.ajax({
                        url: 'gt/opm/workSheet/admin/receive', //请求地址
                        type: "put", //提交的方式S
                        data: str,
                        dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                        beforeSend: function() {
                            loading=layer.load(2);
                        },
                        ContentType:"application/json;charset=UTF-8",
                        success: function(data) { 
                            layer.closeAll('loading');
                            if (data.flag) {
                                layer.msg(data.msg, { icon: 1, time: 2000});
                                customPageFun(searchstr)
                            } else {
                                layer.msg(data.msg, { icon: 2, time: 2000});
                            }
                        }
                        ,error:function(err){
                        }   
                    })
                }else{
                layer.msg("有不可接受的信息请检查您选中的数据!",{
                    icon:2,
                    time:3000
                })
                }
            }else if(obj.event=="finish"){
                if(statu.indexOf("0")=='-1'&&statu.indexOf("2")=='-1'&&statu.indexOf("3")=='-1'){
                    $.ajax({
                        url: 'gt/opm/workSheet/admin/finish', //请求地址
                        type: "PUT", //提交的方式S
                        data: str,
                        dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                        beforeSend: function() {
                            loading=layer.load(2);
                        },
                        ContentType:"application/json;charset=UTF-8",
                        success: function(data) { 
                            layer.closeAll('loading');
                            if (data.flag) {
                                layer.msg(data.msg, { icon: 1, time: 2000});
                                customPageFun(searchstr)
                            } else {
                                layer.msg(data.msg, { icon: 2, time: 2000});
                            }
                        }
                    })
                }else{
                layer.msg("有不可接受的信息请检查您选中的数据!",{
                    icon:2,
                    time:3000
                })
                }
            }
        }else{
            layer.msg("无数据可进行该操作!",{
                icon:2,
                time:3000
            })
        }
    })
    //日期范围
    laydate.render({
        elem: '#pushTime'
        ,range: true
    });
    var active={
        search:function(){
            var dateL=$("#pushTime").val()
            var startTime=dateL.substr(0,10),
            endTime=dateL.substr(13,dateL.length);
            searchstr.workSheetCode=$("#workSheetCode").val();
            searchstr.workSheetContacts=$("#workSheetContacts").val();
            searchstr.priority=$("#priority option:selected").val();
            searchstr.questionType=$("#questionType option:selected").val();
            searchstr.questionContent=$("#questionContent option:selected").val();
            searchstr.createDept=$("#createDept option:selected").val();
            searchstr.statu=$("#statu option:selected").val();
            searchstr.endDate=endTime;
            searchstr.startDate=startTime;
            customPageFun(searchstr)
        }
    }
   $(".bao_loginbtn").on('click',function(data){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
   })
   
})